<?php

declare(strict_types=1);

namespace App\Chat\Listener;

use App\Chat\Constant\TalkEventConstant;
use App\Chat\Event\TalkEvent;
use App\Chat\Event\TalkMessageRevokeEvent;
use App\CustHub\Model\ChTalkRecord;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;

/**
 * 监听消息撤回事件
 */
#[Listener]
class TalkMessageRevokeListener  implements ListenerInterface
{
    public function listen(): array
    {
        return [
            TalkMessageRevokeEvent::class
        ];
    }

    public function process(object $event): void
    {
        ChTalkRecord::query()
            ->where('robot_id',$event->robotId)
            ->where('msg_unique_identifier',$event->revokeMsgUniqueIdentifier)
            ->update([
                'is_revoke' => 1
            ]);

        $info = ChTalkRecord::query()
            ->where('robot_id',$event->robotId)
            ->where('msg_unique_identifier',$event->revokeMsgUniqueIdentifier)
            ->first();
        if (empty($info)){
            return;
        }

        $info->is_revoke = 1;
        $info->save();

        event(new TalkEvent(TalkEventConstant::EVENT_TALK_REVOKE, [
            'record_id' => $info->id
        ]));
    }

}